package projectmanagement.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import projectmanagement.model.Task;
import projectmanagement.util.ApplicationSecurityManager;
import projectmanager.model.dao.TaskDao;

public class TaskController extends SimpleFormController{
	public static final String TASK_ID = "id";
	
	private TaskDao taskManager;
	private ApplicationSecurityManager applicationSecurityManager;


    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
		ModelAndView modelAndView = new ModelAndView("task");

		String id = request.getParameter(TASK_ID);

		if (id != null && id.trim().length() > 0) {
			Task task = this.taskManager.getTaskById(Integer.parseInt(id));
			
			if (task != null){
				modelAndView.addObject("task", task);
			}else{
				throw new ServletException("Nėra tokios užduoties.");
			}
		} else {
			throw new ServletException("Blogai nurodytas id.");
		}

		return modelAndView;
    }

	public TaskDao getTaskManager() {
		return taskManager;
	}

	public void setTaskManager(TaskDao taskManager) {
		this.taskManager = taskManager;
	}

	public ApplicationSecurityManager getApplicationSecurityManager() {
		return applicationSecurityManager;
	}

	public void setApplicationSecurityManager(
			ApplicationSecurityManager applicationSecurityManager) {
		this.applicationSecurityManager = applicationSecurityManager;
	}
}
